package controllers;

import java.util.Hashtable;
import java.util.Vector;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;

import scheduler.AboutForm;
import scheduler.AddForm;
import scheduler.DayChoiceForm;
import scheduler.DayViewList;
import scheduler.EditForm;
import scheduler.PairViewForm;


import commands.Commands;
import commands.Dispatcher;

import core.Schedule;

public class DisplaysController  implements CommandListener {
	
	private class Item{
		private Displayable m_sender;
		private Displayable m_reciever;
		
		public Item(Displayable sender, Displayable reciever){
			m_sender = sender;
			m_reciever = reciever;
		}
		
		public Displayable getSender() {
			return m_sender;
		}
		public Displayable getReciever() {
			return m_reciever;
		}
	}
	
	private MIDlet m_midlet;
	private Schedule m_schedule;
	private Displayable m_active;
	
	private Hashtable m_items;

	public DisplaysController(MIDlet midlet){
		m_midlet = midlet;
		m_schedule = new Schedule();
		m_items = new Hashtable();
		initialize();		
	}
	
	public void setActive(Displayable display){
		Display current = Display.getDisplay(m_midlet);
		m_active = display;
		current.setCurrent(m_active);
	}
	
	public Schedule getSchedule(){
		return m_schedule;
	}
	
	public void commandAction(Command cmd, Displayable sender) {
		String cmdLbl = cmd.getLabel();

		if (cmdLbl.equals(Commands.Exit)){
			m_midlet.notifyDestroyed();
		}
		
		if (!m_items.containsKey(cmdLbl)){
			return;
		}
		
		Vector cmdVector = (Vector)m_items.get(cmdLbl);
		for (int i = 0; i < cmdVector.size(); ++i){
			Item item = (Item)cmdVector.elementAt(i);
			if(m_active != item.getSender()){
				continue;
			}
			setActive(item.getReciever());
		}		
	}
	
	private void initialize(){
		DayViewList dayView = new DayViewList(); 
		AddForm addForm = new AddForm();
		DayChoiceForm dayChoiceForm = new DayChoiceForm();
		PairViewForm pairView = new PairViewForm();
		EditForm editForm = new EditForm();
		AboutForm aboutForm = new AboutForm(); 

		Vector startVector = new Vector();
		startVector.addElement(new Item(null, dayView));
		
		m_items.put(Commands.Start, startVector);
		Dispatcher.getInstance().registerListener(this, Commands.Start);
		
		Vector cancelVector = new Vector();
		cancelVector.addElement(new Item(addForm, dayView));
		
		m_items.put(Commands.Cancel, cancelVector);
		Dispatcher.getInstance().registerListener(this, Commands.Cancel);
				
		Vector newEntryVector = new Vector();
		newEntryVector.addElement(new Item(addForm, dayView));

		m_items.put(Commands.NewEntry, newEntryVector);
		Dispatcher.getInstance().registerListener(this, Commands.NewEntry);
				
		Vector addVector = new Vector();
		addVector.addElement(new Item(dayView, addForm));

		m_items.put(Commands.Add, addVector);
		Dispatcher.getInstance().registerListener(this, Commands.Add);	
		
		Vector dayChoiceVector = new Vector();
		dayChoiceVector.addElement(new Item(dayView, dayChoiceForm));

		m_items.put(Commands.DayChoice, dayChoiceVector);
		Dispatcher.getInstance().registerListener(this, Commands.DayChoice);
		
		Vector dayChoiceOkVector = new Vector();
		dayChoiceOkVector.addElement(new Item(dayChoiceForm, dayView));

		m_items.put(Commands.DayChoiceOk, dayChoiceOkVector);
		Dispatcher.getInstance().registerListener(this, Commands.DayChoiceOk);
		
		Vector pairViewVector = new Vector();
		pairViewVector.addElement(new Item(dayView, pairView));

		m_items.put(Commands.PairView, pairViewVector);
		Dispatcher.getInstance().registerListener(this, Commands.PairView);
		
		Vector pairViewOkVector = new Vector();
		pairViewOkVector.addElement(new Item(pairView, dayView));

		m_items.put(Commands.PairViewOk, pairViewOkVector);
		Dispatcher.getInstance().registerListener(this, Commands.PairViewOk);
		
		Vector editVector = new Vector();
		editVector.addElement(new Item(dayView, editForm));

		m_items.put(Commands.Edit, editVector);
		Dispatcher.getInstance().registerListener(this, Commands.Edit);
		
		Vector editBackVector = new Vector();
		editBackVector.addElement(new Item(editForm, dayView));

		m_items.put(Commands.EditCancel, editBackVector);
		Dispatcher.getInstance().registerListener(this, Commands.EditCancel);
		
		Vector editEntryVector = new Vector();
		editEntryVector.addElement(new Item(editForm, dayView));

		m_items.put(Commands.EditEntry, editEntryVector);
		Dispatcher.getInstance().registerListener(this, Commands.EditEntry);
		
		Vector aboutVector = new Vector();
		aboutVector.addElement(new Item(dayView, aboutForm));

		m_items.put(Commands.About, aboutVector);
		Dispatcher.getInstance().registerListener(this, Commands.About);
		
		Vector aboutOkVector = new Vector();
		aboutOkVector.addElement(new Item(aboutForm, dayView));

		m_items.put(Commands.AboutOk, aboutOkVector);
		Dispatcher.getInstance().registerListener(this, Commands.AboutOk);

		Dispatcher.getInstance().registerListener(this, Commands.Exit);				
	}
}
